package com.che.software.testato.business;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.che.software.testato.business.exception.ProjectCreationManagerException;
import com.che.software.testato.business.exception.ProjectSearchManagerException;
import com.che.software.testato.domain.dao.IProjectDAO;
import com.che.software.testato.domain.dao.jdbc.exception.ProjectCreationDAOException;
import com.che.software.testato.domain.dao.jdbc.exception.ProjectSearchDAOException;
import com.che.software.testato.domain.entity.Project;
import com.che.software.testato.domain.entity.search.ProjectSearch;

/**
 * Manager dedicated to the projects.
 * 
 * @author Clement HELIOU (clement.heliou@che-software.com).
 * @copyright Che Software.
 * @license GNU General Public License.
 * @since July, 2011.
 * 
 *        This file is part of Testato.
 * 
 *        Testato is free software: you can redistribute it and/or modify it
 *        under the terms of the GNU General Public License as published by the
 *        Free Software Foundation, either version 3 of the License, or (at your
 *        option) any later version.
 * 
 *        Testato is distributed in the hope that it will be useful, but WITHOUT
 *        ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 *        FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 *        for more details.
 * 
 *        You should have received a copy of the GNU General Public License
 *        along with Testato. If not, see <http://www.gnu.org/licenses/>.
 * 
 *        Testato's logo is a creation of Arrioch
 *        (http://arrioch.deviantart.com/) and it's distributed under the terms
 *        of the Creative Commons License.
 */
@Service("projectManager")
public class ProjectManager {

	/**
	 * Constants.
	 */
	private static final Logger LOGGER = Logger.getLogger(ProjectManager.class);

	/**
	 * Members.
	 */
	@Autowired
	private IProjectDAO projectDAO;

	/**
	 * Creates a project from his name.
	 * 
	 * @author Clement HELIOU (clement.heliou@che-software.com).
	 * @param name the name of the project to create.
	 * @since July, 2011.
	 * @throws ProjectCreationManagerException if an error occurs during the
	 *         creation.
	 */
	public void createProjectFromName(String name) throws ProjectCreationManagerException {
		LOGGER.debug("createProjectFromName(" + name + ").");
		try {
			projectDAO.createProjectFromName(name);
		} catch (ProjectCreationDAOException e) {
			throw new ProjectCreationManagerException(e);
		}
	}

	/**
	 * Checks if a project with given name is still existing.
	 * 
	 * @author Clement HELIOU (clement.heliou@che-software.com).
	 * @param name the project name to check
	 * @return true if this name is already used, else false.
	 * @since July, 2011.
	 * @throws ProjectSearchManagerException
	 */
	public boolean isProjectExists(String name) throws ProjectSearchManagerException {
		LOGGER.debug("isProjectExists(" + name + ").");
		try {
			return projectDAO.isProjectExists(name);
		} catch (ProjectSearchDAOException e) {
			throw new ProjectSearchManagerException(e);
		}
	}

	/**
	 * Project search from a bean of criterions.
	 * 
	 * @author Clement HELIOU (clement.heliou@che-software.com).
	 * @param searchBean the criterions to use for the search.
	 * @return the resulting object list.
	 * @since July, 2011.
	 * @throws ProjectSearchManagerException if an error occurs during the
	 *         search.
	 */
	public List<Project> searchProject(ProjectSearch searchBean) throws ProjectSearchManagerException {
		LOGGER.debug("searchProject().");
		try {
			return projectDAO.searchProject(searchBean);
		} catch (ProjectSearchDAOException e) {
			throw new ProjectSearchManagerException(e);
		}
	}

	/**
	 * Getter for the private field value projectDAO.
	 * 
	 * @return the projectDAO field value.
	 */
	public IProjectDAO getProjectDAO() {
		return projectDAO;
	}

	/**
	 * Setting a value to the projectDAO field.
	 * 
	 * @param projectDAO the value to set.
	 */
	public void setProjectDAO(IProjectDAO projectDAO) {
		this.projectDAO = projectDAO;
	}
}